﻿#include "shujushangbao.h"
#include "ui_shujushangbao.h"
#include <QDebug>
#include <QModelIndex>
#include <QTableWidget>
#include <QFileDialog>
#include <QDesktopServices>
#include <QMessageBox>
//#include <QAxObject>
#include <QDomDocument>
#include <math.h>
#include <QDate>
#include <QHeaderView>
#include <QSqlQuery>
#include <QDebug>
#include <QLatin1String>
#include <QTableWidget>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QSqlDriver>
#include <QSqlRecord>
#include <QSqlError>
#include <QTextCodec>
#include <QString>

ShuJuShangBao::ShuJuShangBao(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::ShuJuShangBao)
{
    QTextCodec::setCodecForTr(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF8"));
    QTextCodec::setCodecForCStrings(QTextCodec::codecForName("UTF8"));
    ui->setupUi(this);
    ui->table1->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->table2->setSelectionBehavior(QAbstractItemView::SelectRows);
    ui->table1->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->table2->setEditTriggers(QAbstractItemView::NoEditTriggers);
    //connect(this,SIGNAL(recvData2(QString)),this,SLOT(on_pushButton_clicked(QString)));
    ui->addone->setEnabled(false);
    ui->delone->setEnabled(false);
    ui->addall->setEnabled(false);
    ui->delall->setEnabled(false);
}

ShuJuShangBao::~ShuJuShangBao()
{
    delete ui;
}

void ShuJuShangBao::recvData2(QString id)//接受数据在表格显示数据
{
    QSqlQuery query;
    QSqlQuery changeminzu;//通过查询民族代码表改变民族
    QSqlQuery changezhiwu;//查询职务表，通过IDNum选择现任职务
    QSqlQuery selectschoolmajor;//选择全日制专业
    QSqlQuery selectworkmajor;//选择在职专业
    QSqlQuery selectpolitic;//通过查询政治面貌代码表改变政治面貌
    QSqlQuery selectunit;//选择单位
    QSqlQuery selectjiguan;
    QSqlQuery selectschoolstart;
    QSqlQuery selectworkstart;
    //query.exec("select * from ljtt");
    //QSqlRecord rec=query.record();//列数
    //qDebug()<<QObject::tr("ljtt 字段：")<<rec.count();
    //table1= new QTableWidget(this);//一列一列输入的信息（竖这输入的信息）
    //int s=1;
    query.exec(QString("select CadreName,Birth,IDNum,Sex,UnitID,NationID,PartyTime,JoinWorkTime,Health,NativePlace,PoliticID from CadreInfo where IDNum='%1'").arg(id));//where定义查询条件
    //query.exec("select CadreName,Birth,IDNum,Sex,UnitID,NationID,PartyTime,JoinWorkTime,Health,NativePlace,PoliticID from CadreInfo");//where定义查询条件
    //姓名 出生时间 证件号 性别 单位 职务 民族 入党时间 参加工作时间 健康状况 籍贯 政治面貌 全日专业 在职专业
    //0   1       2      3   4    ?   5    6       7          8       9     10      ？      ？  query.value(?)
    //0   1       2      3   4    5   6    7       8          9       10    11      12      13  table->setItem(rowcount,?,new QTableWidgetItem(?));
    //职务需要查询表增加
    //  query.exec("select * from ljtt");//where定义查询条件
    while(query.next())
    {
        int rowcount=ui->table1->rowCount();//有几列
        ui->table1->insertRow(rowcount);//加一行table

        QString CadreName=query.value(0).toString();//0
        ui->table1->setItem(rowcount,0,new QTableWidgetItem(CadreName));

        QString Birth=query.value(1).toString();//1
        ui->table1->setItem(rowcount,1,new QTableWidgetItem(Birth));

        QString IDNum=query.value(2).toString();//2
        ui->table1->setItem(rowcount,2,new QTableWidgetItem(IDNum));

        QString Sex=query.value(3).toString();//3性别
        if(Sex=="1"){
            Sex="男";
        }else if(Sex=="0")
        {
            Sex="女";
        }
        ui->table1->setItem(rowcount,3,new QTableWidgetItem(Sex));
        QString UnitID=query.value(4).toString();//4单位，查询单位信息表
        selectunit.exec(QString("select unitname from unit where unitid='%1'").arg(UnitID));
        QString unitname="NULL";
        while(selectunit.next()){
            unitname=selectunit.value(0).toString();
        }
        ui->table1->setItem(rowcount,4,new QTableWidgetItem(unitname));
        //*********************************************************************//职务需要查询表
        changezhiwu.exec(QString("select PostName from Post where IDnum='%1'and QualifyType=0").arg(IDNum));
        QString zhiwu;

        while(changezhiwu.next()){
            zhiwu=changezhiwu.value(0).toString();
            //qDebug()<<zhiwu;
        }
        ui->table1->setItem(rowcount,5,new QTableWidgetItem(zhiwu));
        //*********************************************************************
        QString NationID=query.value(5).toString();//5民族代码换为民族

        changeminzu.exec(QString("select Nation from NationCode where NationID ='%1'").arg(NationID));
        QString Nation="NULL";
        while(changeminzu.next())
        {
            Nation=changeminzu.value(0).toString();

        }ui->table1->setItem(rowcount,6,new QTableWidgetItem(Nation));
        QString PartyTime=query.value(6).toString();//6
        ui->table1->setItem(rowcount,7,new QTableWidgetItem(PartyTime));

        QString JoinWorkTime=query.value(7).toString();//7
        ui->table1->setItem(rowcount,8,new QTableWidgetItem(JoinWorkTime));

        QString Health=query.value(8).toString();//8健康
        ui->table1->setItem(rowcount,9,new QTableWidgetItem(Health));

        QString NativePlace=query.value(9).toString();//9籍贯需要代码表切换
        selectjiguan.exec(QString("select AdminArea from AdminAreaCode where AdminAreaID='%1'").arg(NativePlace));
        while(selectjiguan.next()){
            NativePlace=selectjiguan.value(0).toString();
        }
        ui->table1->setItem(rowcount,10,new QTableWidgetItem(NativePlace));

        QString PoliticID=query.value(10).toString();//10政治面貌
        selectpolitic.exec(QString("select Politic from PoliticCode where PoliticID ='%1'").arg(PoliticID));
        QString Politic="NULL";
        while(selectpolitic.next())
        {
            Politic=selectpolitic.value(0).toString();
        }
        ui->table1->setItem(rowcount,11,new QTableWidgetItem(Politic));



        QDate sssss;
        selectschoolstart.exec(QString("select max(StartTime) from Edu where IDNum='%1' and WorkORSchool=1").arg(IDNum));
        while(selectschoolstart.next()){
            sssss=selectschoolstart.value(0).toDate();
        }

        QString ssss=sssss.toString("yyyy-MM-dd");

        selectschoolmajor.exec(QString("select Major,School,StartTime from Edu where IDNum ='%1'and WorkORSchool=1 and StartTime='%2'").arg(IDNum).arg(ssss));//全日1
        QString SchoolMajor="NULL";
        QString Schooleduname="NULL";
        QDate Schoolstart;
        QString Schoolstarttime;
        while(selectschoolmajor.next())
        {
            SchoolMajor=selectschoolmajor.value(0).toString();//全日专业需要查询数据
            Schooleduname=selectschoolmajor.value(1).toString();
            Schoolstart=selectschoolmajor.value(2).toDate();
            Schoolstarttime=Schoolstart.toString("yyyy-MM-dd");
        }
        ui->table1->setItem(rowcount,12,new QTableWidgetItem(SchoolMajor));
        ui->table1->setItem(rowcount,14,new QTableWidgetItem(Schooleduname));
        ui->table1->setItem(rowcount,16,new QTableWidgetItem(Schoolstarttime));

        QDate sss;
        selectworkstart.exec(QString("select max(StartTime) from Edu where IDNum='%1' and WorkORSchool=0").arg(IDNum));
        while(selectworkstart.next()){
            sss=selectworkstart.value(0).toDate();

        }

        QString ss=sss.toString("yyyy-MM-dd");

        selectworkmajor.exec(QString("select Major,School,StartTime from Edu where IDNum ='%1'and WorkORSchool=0 and StartTime='%2'").arg(IDNum).arg(ss));//在职0
        QString WorkMajor="NULL";
        QString Workeduname="NULL";
        QDate Workstart;
        QString Workstarttime;
        while(selectworkmajor.next())
        {
            WorkMajor=selectworkmajor.value(0).toString();//在职专业需要查询数据
            Workeduname=selectworkmajor.value(1).toString();
            Workstart=selectworkmajor.value(2).toDate();
            Workstarttime=Workstart.toString("yyyy-MM-dd");
        }
        ui->table1->setItem(rowcount,13,new QTableWidgetItem(WorkMajor));
        ui->table1->setItem(rowcount,15,new QTableWidgetItem(Workeduname));
        ui->table1->setItem(rowcount,17,new QTableWidgetItem(Workstarttime));

    }

    int row111=ui->table1->rowCount();//删除重复的数据

    for(int i=0;i<row111;i++)
    {
        QVariant idata = ui->table1->item(i,2)->text();
        QString istring=idata.toString();

        //        qDebug()<<"i"<<i;
        for(int j=i+1;j<row111;j++)
        {
            //记录j的值，循环后，删除j的行
            if(ui->table1->item(j,2)!=NULL){
            QVariant jdata = ui->table1->item(j,2)->text();
            QString jstring=jdata.toString();

            if(istring==jstring)
            {
                //                qDebug()<<"j"<<j;
                ui->table1->removeRow(j);
                row111=ui->table1->rowCount();
                //                qDebug()<<"row"<<row;
                j=j-1;
            }
            }
        }
    }


    ui->addall->setEnabled(true);

}

void ShuJuShangBao::on_check_clicked()//组合查询功能页面？？？？？？？要不要在每次查询之前都清空table1中的内容
{

    //    int deletetable1 =  ui->table1->rowCount();//删除所有table1中数据
    //    for(int i = 0;i < deletetable1;i++)
    //    {
    //        ui->table1->removeRow(0);
    //    }
    if(ZuHeChaXun::flog==0){
        ZuHeChaXun::flog=1;
        zuhechaxun=new ZuHeChaXun();
        connect(zuhechaxun,SIGNAL(sendData2(QString)),this,SLOT(recvData2(QString)));//接受同名干部IDNum
        connect(zuhechaxun,SIGNAL(sendData3(QString)),this,SLOT(recvData2(QString)));//接受组合查询干部IDNum
        zuhechaxun->show();
    }

}

void ShuJuShangBao::on_pushButton_6_clicked()//导出，日志！！！！！
{
    QString filename = QFileDialog::getSaveFileName( this, "Save", "", "*.xml" );
    QFile file( filename );
    if( !file.open(QFile::WriteOnly | QFile::Text) )
    {
        return;
    }
    //    QDomElement gen=document.createElement("items");
    //    //int s=2;//可以直接通过这个导入
    //    gen.setAttribute("hang",1);
    //    all.appendChild(gen);

    //    QDomElement item1 = document.createElement( "item" );
    //    //int s=1;
    //        item1.setAttribute( "lie", "id" );
    //        item1.setAttribute( "date", s);
    //        gen.appendChild( item1 );

    //        QDomElement item2 = document.createElement( "item" );
    //        item2.setAttribute( "lie", "num" );
    //        item2.setAttribute( "date", tr("哈货"));
    //        gen.appendChild( item2 );

    //        QDomElement item3 = document.createElement( "item" );
    //        item3.setAttribute( "lie", "num");
    //        item3.setAttribute( "date", "12345" );
    //        gen.appendChild( item3 );

    //        QTextStream out( &file );
    //            document.save( out, 4 );
    //            file.close();

    //int rows2=ui->table2->rowCount();//表2的行
    //*****************************************************
    QSqlQuery query;
    QSqlQuery changeunit;//转换单位代码
    QSqlQuery changepolitic;//转行政治面貌
    QSqlQuery changejiguan;
    QDomDocument doc;
    QString strHeader( "version=\"1.0\" encoding=\"UTF-8\"" );
    doc.appendChild( doc.createProcessingInstruction("xml", strHeader));
    QDomElement table=doc.createElement("table");//创建根节点
    doc.appendChild(table);
    int row=ui->table2->rowCount();
    for (int i=0;i<row;i++)
    {
        int j=0;
        QDomElement hang=doc.createElement("hang");
        table.appendChild(hang);
        int hangnum=i+1;
        hang.setAttribute("hang",hangnum);
        //            QDomElement hangdate1=doc.createElement("hangdate");
        //            hang.appendChild(hangdate1);
        //            QString hangdate = QString::number(i, 10);
        //            QDomText hangtext=doc.createTextNode(hangdate);
        //            hangdate1.appendChild(hangtext);
        QDomElement name=doc.createElement("name");//姓名
        hang.appendChild(name);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var1= ui->table2->item(i,j)->text();
        QString namedate=var1.toString();
        QDomText nametext=doc.createTextNode(namedate);
        name. appendChild(nametext);
        }
        j++;
        QDomElement birth=doc.createElement("birth");//生日
        hang.appendChild(birth);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var2 = ui->table2->item(i,j)->text();
        QString birthdate=var2.toString();
        QDomText birthtext=doc.createTextNode(birthdate);
        birth. appendChild(birthtext);
        }
        j++;
        QDomElement id=doc.createElement("id");//证件号
        hang.appendChild(id);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var3 = ui->table2->item(i,j)->text();
        QString iddate=var3.toString();
        QDomText idtext=doc.createTextNode(iddate);
        id. appendChild(idtext);
        }
        j++;
        QDomElement sex=doc.createElement("sex");//性别
        hang.appendChild(sex);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var4 = ui->table2->item(i,j)->text();
        QString sexdate=var4.toString();

        if(sexdate=="男"){
            sexdate="1";
        }else if(sexdate=="女")
        {
            sexdate="0";
        }
        QDomText sextext=doc.createTextNode(sexdate);
        sex. appendChild(sextext);
        }
        j++;

        QDomElement nuit=doc.createElement("nuit");//单位
        hang.appendChild(nuit);
if(ui->table2->item(i,j)!=NULL){
        QVariant var5 = ui->table2->item(i,j)->text();
        QString nuitdate=var5.toString();
        changeunit.exec(QString("select unitid from unit where unitname='%1'").arg(nuitdate));
        while(changeunit.next()){
            nuitdate=changeunit.value(0).toString();
        }

        QDomText nuittext=doc.createTextNode(nuitdate);
        nuit. appendChild(nuittext);
}
        j++;
        QDomElement zhiwu=doc.createElement("zhiwu");//职务
        hang.appendChild(zhiwu);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var6 = ui->table2->item(i,j)->text();
        QString zhiwudate=var6.toString();
        QDomText zhiwutext=doc.createTextNode(zhiwudate);
        zhiwu. appendChild(zhiwutext);
        }
        j++;
        QDomElement minzu=doc.createElement("minzu");//民族换为代码
        hang.appendChild(minzu);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var7 = ui->table2->item(i,j)->text();
        QString minzudate=var7.toString();

        query.exec(QString("select NationID from NationCode where Nation ='%1'").arg(minzudate));
        QString minzucode;
        QDomText minzutext;
        while(query.next()){
            minzucode=query.value(0).toString();

        }
        minzutext=doc.createTextNode(minzucode);
        minzu. appendChild(minzutext);
        }
        j++;

        QDomElement rudang=doc.createElement("rudang");//入党时间
        hang.appendChild(rudang);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var8 = ui->table2->item(i,j)->text();
        QString rudangdate=var8.toString();
        QDomText rudangtext=doc.createTextNode(rudangdate);
        rudang. appendChild(rudangtext);
        }
        j++;
        QDomElement joinwork=doc.createElement("joinwork");//参加工作时间
        hang.appendChild(joinwork);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var9 = ui->table2->item(i,j)->text();
        QString joinworkdate=var9.toString();
        QDomText joinworktext=doc.createTextNode(joinworkdate);
        joinwork. appendChild(joinworktext);
        }
        j++;

        QDomElement health=doc.createElement("health");//健康状况
        hang.appendChild(health);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var10 = ui->table2->item(i,j)->text();
        QString healthdate=var10.toString();
        QDomText healthtext=doc.createTextNode(healthdate);
        health. appendChild(healthtext);
        }
        j++;
        QDomElement jiguan=doc.createElement("jiguan");//籍贯
        hang.appendChild(jiguan);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var11 = ui->table2->item(i,j)->text();
        QString jiguandate=var11.toString();
        changejiguan.exec(QString("select AdminAreaID from AdminAreaCode where AdminArea='%1'").arg(jiguandate));
        while(changejiguan.next()){
            jiguandate=changejiguan.value(0).toString();
        }
        QDomText jiguantext=doc.createTextNode(jiguandate);
        jiguan. appendChild(jiguantext);
}
        j++;
        QDomElement politic=doc.createElement("politic");//政治面貌转换为代码
        hang.appendChild(politic);
if(ui->table2->item(i,j)!=NULL){

        QVariant var12 = ui->table2->item(i,j)->text();
        QString politicdate=var12.toString();
        changepolitic.exec(QString("select PoliticID from PoliticCode where Politic ='%1'").arg(politicdate));
        while(changepolitic.next()){
            politicdate=changepolitic.value(0).toString();
        }
        QDomText politictext=doc.createTextNode(politicdate);
        politic. appendChild(politictext);
}
        j++;
        QDomElement schoolmaior=doc.createElement("schoolmajor");//全日专业
        hang.appendChild(schoolmaior);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var13 = ui->table2->item(i,j)->text();
        QString schoolmaiordate=var13.toString();
        QDomText schoolmaiortext=doc.createTextNode(schoolmaiordate);
        schoolmaior. appendChild(schoolmaiortext);
}
        j++;
        QDomElement workmaior=doc.createElement("workmajor");//在职专业
        hang.appendChild(workmaior);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var14 = ui->table2->item(i,j)->text();
        QString workmaiordate=var14.toString();
        QDomText workmaiortext=doc.createTextNode(workmaiordate);
        workmaior. appendChild(workmaiortext);
}
        j++;
        QDomElement schoolname=doc.createElement("schoolname");//全日学校
        hang.appendChild(schoolname);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var15 = ui->table2->item(i,j)->text();
        QString schoolnamedate=var15.toString();
        QDomText schoolnametext=doc.createTextNode(schoolnamedate);
        schoolname. appendChild(schoolnametext);
}
        j++;
        QDomElement workname=doc.createElement("workname");//在职学校
        hang.appendChild(workname);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var16 = ui->table2->item(i,j)->text();
        QString worknamedate=var16.toString();
        QDomText worknametext=doc.createTextNode(worknamedate);
        workname. appendChild(worknametext);

}

        j++;
        QDomElement schooltime=doc.createElement("schooltime");//全日学校
        hang.appendChild(schooltime);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var17 = ui->table2->item(i,j)->text();
        QString schooltimedate=var17.toString();
        QDomText schooltimetext=doc.createTextNode(schooltimedate);
        schooltime. appendChild(schooltimetext);
}
        j++;
        QDomElement worktime=doc.createElement("worktime");//在职学校
        hang.appendChild(worktime);
        if(ui->table2->item(i,j)!=NULL){
        QVariant var18 = ui->table2->item(i,j)->text();
        QString worktimedate=var18.toString();
        QDomText worktimetext=doc.createTextNode(worktimedate);
        worktime. appendChild(worktimetext);

}












        //        //date.setAttribute("hang",i);

        //        QVariant var1= ui->table2->item(i,j)->text();
        //        QString id=var1.toString();
        //        //date.setAttribute("id",id);
        //        j++;

        //        QVariant var2 = ui->table2->item(i,j)->text();
        //        QString name=var2.toString();
        //        //date.setAttribute("name",name);
        //        j++;

        //        QVariant var3 = ui->table2->item(i,j)->text();
        //        QString num=var3.toString();
        //date.setAttribute("num",num);

        //table.appendChild(date);
    }
    QTextStream out( &file );
    doc.save( out, 4 );
    file.close();

    //*******************************************************
    //QDomDocument document;
    //QString strHeader( "version=\"1.0\" encoding=\"UTF-8\"" );
    //document.appendChild( document.createProcessingInstruction("xml", strHeader));

    //QDomElement all=document.createElement("table");//创建根节点
    //int s=2;//可以直接通过这个导入
    //all.setAttribute("all",1);
    //document.appendChild(all);//将元素节点放入根节点
    //for(int j=0;j<2;j++)     //遍历tabel2的所有数据
    //{
    //    QDomElement gen=document.createElement("items");//第j行数据
    //    //int s=2;//可以直接通过这个导入
    //    int hang=j;
    //    gen.setAttribute("hang",hang);
    //    all.appendChild(gen);

    //    for(int i=0;i<3;i++)//输入每一行的数据
    //    {
    //        QVariant var = ui->table2->item(j,i)->text();//item(j,i)->item(0,i)
    //        QString ha=var.toString();
    //        if(i==0){
    //            QDomElement item1 = document.createElement( "item" );
    //            //int s=1;
    //            item1.setAttribute( "date", ha);
    //            item1.setAttribute( "lie", "id" );
    //            gen.appendChild( item1 );
    //        }
    //        else if(i==1){
    //            QDomElement item2 = document.createElement( "item" );
    //            item2.setAttribute( "date", ha);
    //            item2.setAttribute( "lie", "name" );
    //            gen.appendChild( item2 );
    //        }else if(i==2){
    //            QDomElement item3 = document.createElement( "item" );
    //            item3.setAttribute( "date", ha );
    //            item3.setAttribute( "lie", "num");
    //            gen.appendChild( item3 );
    //        }
    //    }
    //}
    //QTextStream out( &file );
    //document.save( out, 4 );
    //file.close();

}

void ShuJuShangBao::on_delone_clicked()//从右向左交换选中的一行数据      <
{

    int row = ui-> table2 ->currentIndex().row();//选中table1的行数字
    int rows2=ui->table1->rowCount();//表2的行
    int lie1=ui->table2->columnCount();
    ui->table1->insertRow(rows2);
    for(int i=0;i<lie1;i++)//遍历选中行的所有数据
    {
        if(ui->table2->item(row,i)!=NULL){
        QVariant var = ui->table2->item(row,i)->text();
        QString ha=var.toString();
        ui->table1->setItem(rows2,i,new QTableWidgetItem(ha));
        }
    }
    ui->table2->removeRow(row);


    int row2=ui->table2->rowCount();
    if(row2==0)
    {
        ui->delall->setEnabled(false);
    }
    ui->addall->setEnabled(true);
    ui->delone->setEnabled(false);

}

void ShuJuShangBao::on_delall_clicked()//将右侧所有数据交换到左侧       <<
{
    ui->delone->setEnabled(false);

    int rows1=ui->table2->rowCount();//表1的行
    int lie1=ui->table2->columnCount();//表1的列
    for(int i=0;i<rows1;i++)//遍历选中行的所有数据
    {
        int rows2=ui->table1->rowCount();//表2的行
        ui->table1->setRowCount(rows2+1);
        for(int j=0;j<lie1;j++)
        {
            if(ui->table2->item(i,j)!=NULL){
            QString ha = ui->table2->item(i,j)->text();
            ui->table1->setItem(rows2,j,new QTableWidgetItem(ha));
            }
        }
    }


    int rowNum =  ui->table2->rowCount();
    for(int i = 0;i < rowNum;i++)
    {
        ui->table2->removeRow(0);
    }




    ui->delall->setEnabled(false);

    ui->addall->setEnabled(true);
}

void ShuJuShangBao::on_addall_clicked()//将左侧所有数据交换到右侧       >>
{
    QVector<int> ljt;//记录重复的行
    ui->delall->setEnabled(true);
    ui->addone->setEnabled(false);

    int rows1=ui->table1->rowCount();//表1的行
    int lie1=ui->table1->columnCount();//表1的列

    for(int i=0;i<rows1;i++)//遍历选中行的所有数据
    {
        int rows2=ui->table2->rowCount();//表2的行
        QString judge1 = ui->table1->item(i,2)->text();//取左边干部的id，与右边干部id判断是否重复，重复i++
        for(int k=0;k<rows2;k++)
        {
             QString judge2 = ui->table2->item(k,2)->text();
            if(judge1==judge2){
                ljt.append(i);
            }
        }
    }


    int m = ljt.count();

    while(m>0){
        ui->table1->removeRow(ljt[m-1]);

        m--;
    }


    rows1=ui->table1->rowCount();//表1的行

    for(int i=0;i<rows1;i++)//遍历选中行的所有数据
    {



        int rows2=ui->table2->rowCount();//表2的行

        ui->table2->setRowCount(rows2+1);
        for(int j=0;j<lie1;j++)
        {
            if(ui->table1->item(i,j)!=NULL){



            QString ha = ui->table1->item(i,j)->text();
            ui->table2->setItem(rows2,j,new QTableWidgetItem(ha));
            }
        }
    }

    int rowNum11 =  ui->table1->rowCount();
    for(int i = 0;i < rowNum11;i++)
    {
        ui->table1->removeRow(0);
    }


    ui->addall->setEnabled(false);

}

void ShuJuShangBao::on_addone_clicked()//从左向右交换选中的一行数据      >
{
    ui->delall->setEnabled(true);

    int row = ui-> table1 ->currentIndex().row();//选中table1的行数字
    int rows2=ui->table2->rowCount();//表2的行
    int lie1=ui->table1->columnCount();
    ui->table2->insertRow(rows2);
    for(int i=0;i<lie1;i++)//遍历选中行的所有数据
    {
        if(ui->table1->item(row,i)!=NULL){
        QVariant var = ui->table1->item(row,i)->text();
        QString ha=var.toString();
        ui->table2->setItem(rows2,i,new QTableWidgetItem(ha));
        }
    }
    ui->table1->removeRow(row);


    ui->addone->setEnabled(false);
    if(ui->table1->rowCount()==0){
        ui->addall->setEnabled(false);
    }

}

void ShuJuShangBao::on_pushdate_clicked()//向table1中写入全部干部信息
{
    int deletetable1 =  ui->table1->rowCount();//删除所有table1的数据
    for(int i = 0;i < deletetable1;i++)
    {
        ui->table1->removeRow(0);
    }
    QSqlQuery query;
    QSqlQuery changeminzu;//通过查询民族代码表改变民族
    QSqlQuery changezhiwu;//查询职务表，通过IDNum选择现任职务
    QSqlQuery selectschoolmajor;//选择全日制专业
    QSqlQuery selectworkmajor;//选择在职专业
    QSqlQuery selectpolitic;//通过查询政治面貌代码表改变政治面貌
    QSqlQuery selectunit;//选择单位
    QSqlQuery selectjiguan;
    QSqlQuery selectschoolstart;
    QSqlQuery selectworkstart;
    //query.exec("select * from ljtt");
    //QSqlRecord rec=query.record();//列数
    //qDebug()<<QObject::tr("ljtt 字段：")<<rec.count();

    //table1= new QTableWidget(this);//一列一列输入的信息（竖这输入的信息）
    //int s=1;

    //query.exec(QString("select CadreName,Birth,IDNum,Sex,UnitID,NationID,PartyTime,JoinWorkTime,Health,NativePlace,PoliticID from CadreInfo where IDNum='%1'").arg(id));//where定义查询条件

    query.exec("select CadreName,Birth,IDNum,Sex,UnitID,NationID,PartyTime,JoinWorkTime,Health,NativePlace,PoliticID from CadreInfo");//where定义查询条件
    //姓名 出生时间 证件号 性别 单位 职务 民族 入党时间 参加工作时间 健康状况 籍贯 政治面貌 全日专业 在职专业
    //0   1       2      3   4    ?   5    6       7          8       9     10      ？      ？  query.value(?)
    //0   1       2      3   4    5   6    7       8          9       10    11      12      13  table->setItem(rowcount,?,new QTableWidgetItem(?));
    //职务需要查询表增加
    //  query.exec("select * from ljtt");//where定义查询条件

    while(query.next())
    {
        int rowcount=ui->table1->rowCount();//有几列
        ui->table1->insertRow(rowcount);//加一行table

        QString CadreName=query.value(0).toString();//0
        ui->table1->setItem(rowcount,0,new QTableWidgetItem(CadreName));

        QString Birth=query.value(1).toString();//1
        ui->table1->setItem(rowcount,1,new QTableWidgetItem(Birth));

        QString IDNum=query.value(2).toString();//2
        ui->table1->setItem(rowcount,2,new QTableWidgetItem(IDNum));

        QString Sex=query.value(3).toString();//3性别
        if(Sex=="1"){
            Sex="男";
        }else if(Sex=="0")
        {
            Sex="女";
        }
        ui->table1->setItem(rowcount,3,new QTableWidgetItem(Sex));
        QString UnitID=query.value(4).toString();//4单位，查询单位信息表
        selectunit.exec(QString("select unitname from unit where unitid='%1'").arg(UnitID));
        QString unitname="NULL";
        while(selectunit.next()){
            unitname=selectunit.value(0).toString();
        }
        ui->table1->setItem(rowcount,4,new QTableWidgetItem(unitname));
        //*********************************************************************//职务需要查询表
        changezhiwu.exec(QString("select PostName from Post where IDnum='%1'and QualifyType=0").arg(IDNum));
        QString zhiwu;
        while(changezhiwu.next()){
            zhiwu=changezhiwu.value(0).toString();
            //qDebug()<<zhiwu;
        }
        ui->table1->setItem(rowcount,5,new QTableWidgetItem(zhiwu));
        //*********************************************************************
        QString NationID=query.value(5).toString();//5民族代码换为民族

        changeminzu.exec(QString("select Nation from NationCode where NationID ='%1'").arg(NationID));
        QString Nation="NULL";
        while(changeminzu.next())
        {
            Nation=changeminzu.value(0).toString();

        }ui->table1->setItem(rowcount,6,new QTableWidgetItem(Nation));
        QString PartyTime=query.value(6).toString();//6
        ui->table1->setItem(rowcount,7,new QTableWidgetItem(PartyTime));

        QString JoinWorkTime=query.value(7).toString();//7
        ui->table1->setItem(rowcount,8,new QTableWidgetItem(JoinWorkTime));

        QString Health=query.value(8).toString();//8健康
        ui->table1->setItem(rowcount,9,new QTableWidgetItem(Health));

        QString NativePlace=query.value(9).toString();//9
        selectjiguan.exec(QString("select AdminArea from AdminAreaCode where AdminAreaID='%1'").arg(NativePlace));
        while(selectjiguan.next()){
            NativePlace=selectjiguan.value(0).toString();
        }
        ui->table1->setItem(rowcount,10,new QTableWidgetItem(NativePlace));

        QString PoliticID=query.value(10).toString();//10政治面貌
        selectpolitic.exec(QString("select Politic from PoliticCode where PoliticID ='%1'").arg(PoliticID));
        QString Politic="NULL";
        while(selectpolitic.next())
        {
            Politic=selectpolitic.value(0).toString();
        }
        ui->table1->setItem(rowcount,11,new QTableWidgetItem(Politic));



        QDate sssss;
        selectschoolstart.exec(QString("select max(StartTime) from Edu where IDNum='%1' and WorkORSchool=1").arg(IDNum));
        while(selectschoolstart.next()){
            sssss=selectschoolstart.value(0).toDate();

        }

        QString ssss=sssss.toString("yyyy-MM-dd");

        selectschoolmajor.exec(QString("select Major,School,StartTime from Edu where IDNum ='%1'and WorkORSchool=1 and StartTime='%2'").arg(IDNum).arg(ssss));//全日1
        QString SchoolMajor="NULL";
        QString Schooleduname="NULL";
        QDate Schoolstart;
        QString Schoolstarttime;
        while(selectschoolmajor.next())
        {
            SchoolMajor=selectschoolmajor.value(0).toString();//全日专业需要查询数据
            Schooleduname=selectschoolmajor.value(1).toString();
            Schoolstart=selectschoolmajor.value(2).toDate();
            Schoolstarttime=Schoolstart.toString("yyyy-MM-dd");
        }
        ui->table1->setItem(rowcount,12,new QTableWidgetItem(SchoolMajor));
        ui->table1->setItem(rowcount,14,new QTableWidgetItem(Schooleduname));
        ui->table1->setItem(rowcount,16,new QTableWidgetItem(Schoolstarttime));

        QDate sss;
        selectworkstart.exec(QString("select max(StartTime) from Edu where IDNum='%1' and WorkORSchool=0").arg(IDNum));
        while(selectworkstart.next()){
            sss=selectworkstart.value(0).toDate();

        }

        QString ss=sss.toString("yyyy-MM-dd");

        selectworkmajor.exec(QString("select Major,School,StartTime from Edu where IDNum ='%1'and WorkORSchool=0 and StartTime='%2'").arg(IDNum).arg(ss));//在职0
        QString WorkMajor="NULL";
        QString Workeduname="NULL";
        QDate Workstart;
        QString Workstarttime;
        while(selectworkmajor.next())
        {
            WorkMajor=selectworkmajor.value(0).toString();//在职专业需要查询数据
            Workeduname=selectworkmajor.value(1).toString();
            Workstart=selectworkmajor.value(2).toDate();
            Workstarttime=Workstart.toString("yyyy-MM-dd");
        }
        ui->table1->setItem(rowcount,13,new QTableWidgetItem(WorkMajor));
        ui->table1->setItem(rowcount,15,new QTableWidgetItem(Workeduname));
        ui->table1->setItem(rowcount,17,new QTableWidgetItem(Workstarttime));

        //        QString name=query.value(1).toString();
        //        ui->table1->setItem(rowcount,1,new QTableWidgetItem(name));
        //        QString num=query.value(2).toString();
        //        ui->table1->setItem(rowcount,2,new QTableWidgetItem(num));
    }
    ui->addall->setEnabled(true);
}

void ShuJuShangBao::on_table1_clicked()//点击table1数据解锁按钮      >
{
    ui->addone->setEnabled(true);
}

void ShuJuShangBao::on_table2_clicked()//点击table2数据解锁按钮     <
{
    ui->delone->setEnabled(true);
}

void ShuJuShangBao::on_pushButton_2_clicked()//添加数据之前选择清空表格
{
    int deletetable1 =  ui->table1->rowCount();//删除所有table1的数据
    for(int i = 0;i < deletetable1;i++)
    {
        ui->table1->removeRow(0);
    }
    ui->addall->setEnabled(false);
}
